package com.example.financechain.controller;


import com.example.financechain.Utils.JsonData;
import com.example.financechain.domain.DTO.AuditParam;
import com.example.financechain.domain.entity.Financing;
import com.example.financechain.service.IFinancingService;
import org.hyperledger.fabric.gateway.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.json.Json;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.cert.CertificateException;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 * 融资信息表 前端控制器
 * </p>
 *
 * @author yy
 * @since 2023-04-19
 */
@RestController
@RequestMapping("private/financing")
public class FinancingController {

    @Autowired
    private  IFinancingService iFinancingService;

    /**
     * 判断是否有进行中的项目
     * @return
     */
    @PostMapping("/create/exist")
    public JsonData financingexsit(){

        return iFinancingService.queryfinancingexsit();
    }


    /**
     * 创建融资 资质认证
     * @param financing
     * @return
     */
    @PostMapping("/create")
    public JsonData financing(@RequestBody Financing financing){

        return iFinancingService.createfiance(financing);

    }

    /**
     * 资料完善 和 提交审核
     * @param financing
     * @return
     */
    @PostMapping("/completeInfo")
    public JsonData fiancingcompleteInfo(@RequestBody Financing financing){
        return iFinancingService.Completeinformation(financing);
    }


    /**
     * 接受签约，上传签约文件 签约文件上链
     * @return
     */
    @PostMapping("/acceptcontract")
    public JsonData acceptContract(@RequestBody Financing financing) throws ContractException, CertificateException, IOException, InvalidKeyException, InterruptedException, TimeoutException {
        return  iFinancingService.acceptContrac(financing);
    }


    /**
     * 银行审查界面
     * @return
     */
    @PostMapping("/query/finance")
    public JsonData queryfinancing (){

        return iFinancingService.queryfinance();

    }

    /**
     * 企业查询融资详情界面
     * @return
     */
    @PostMapping("/query/message")
    public JsonData querymessage (){

        return iFinancingService.querymessage();

    }

    /**
     * 进入审核
     * @param id
     * @return
     */
    @PostMapping("/query/finance/{id}")
    public JsonData queryfinancing (@PathVariable("id") Long id){

        return iFinancingService.queryfinancebyId(id);

    }

    /**
     * 审核结果提交
     * @param auditParam
     * @return
     */
    @PostMapping("/audit")
    public JsonData AuditResults(@RequestBody AuditParam auditParam){

        return iFinancingService.auditResult(auditParam);
    }


    /**
     * 银行发起签约
     * @param financing
     * @return
     */
    @PostMapping("/financing/contract")
    public JsonData Contracting(@RequestBody Financing financing){
        return  iFinancingService.contracting(financing);
    }

}
